<template>
    <view style="height: 100%">
        <van-search :value="value" placeholder="请输入商品关键词搜索" shape="round" @search="onSearch" @change="onChange" @cancel="onCancel" />

        <ul class="db pd015" v-if="list.length != 0">
            <block v-for="(item, index) in list" :key="index">
                <navigator :url="'/pages/order/shopDetail?id=' + item.id" hover-class="none" open-type="navigate" class="pd150 bbf1 df">
                    <image class="img96 br4 mr12"  referrerPolicy='no-referrer' :src="item.thumb" />
                    <view class="f1 df sb fc">
                        <view>
                            <h2 class="f14 c28 f500 db">{{ item.title }}</h2>
                            <!-- <p class="tag tag1 mt4">送积分</p> -->
                        </view>
                        <p class="f14 cff8 f600">¥{{ item.price }}</p>
                    </view>
                </navigator>
            </block>
        </ul>

        <P class="db tc f14 c9 mt40" v-else>没有找到该商品~</P>
    </view>
</template>

<script>
import http from '../../utils/http';
export default {
    data() {
        return {
            list: [],
            page: 1,
            pagesize: 10,
            value: '',
            total: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        this.getList();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.total > this.page) {
            this.setData({
                page: this.page + 1
            });
            this.getList();
        } else {
            uni.showToast({
                title: '没有更多了~',
                icon: 'none'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        onChange(e) {
            this.setData({
                value: e.detail
            });
        },

        onSearch() {
            this.setData({
                list: [],
                page: 1
            });
            this.getList();
        },

        onCancel() {
            this.setData({
                list: [],
                page: 1,
                value: ''
            });
            this.getList();
        },

        getList() {
            let data = {
                page: this.page,
                pages: this.pagesize,
                title: this.value
            };
            http.request('goods/goodsList', data)
                .then((res) => {
                    res.data.list.forEach((i) => {
                        this.list.push(i);
                    });
                    this.setData({
                        list: this.list,
                        total: res.data.pageTotal
                    });
                })
                .catch((res) => {
                    uni.showToast({
                        title: res.message,
                        icon: 'none'
                    });
                });
        }
    }
};
</script>
<style>
@import './search.css';
</style>
